package com.luo.shop.service.impl.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luo.shop.dao.CommentDao;
import com.luo.shop.dao.GoodsDao;
import com.luo.shop.dao.UserDao;
import com.luo.shop.pojo.Comment;
import com.luo.shop.pojo.Goods;
import com.luo.shop.pojo.Page;
import com.luo.shop.pojo.User;
import com.luo.shop.service.admin.CommentAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class CommentAdminServiceImpl implements CommentAdminService {
    @Autowired
    private CommentDao commentDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private GoodsDao goodsDao;


    public Page<Comment> selectAll(Integer pageNum,String info) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        //模糊查询字段
        if (info!=null){
            criteria.andLike("info","%"+info+"%");
        }
        //开启分页
        PageHelper.startPage(pageNum, 5);
        //查询结果集
        List<Comment> comments = commentDao.selectByExample(example);
        //封装结果集对象
        PageInfo<Comment> pageInfo = new PageInfo<Comment>(comments);

        List<Comment> list = pageInfo.getList();
        for (Comment comment : list) {
           comment.setUser(userDao.selectByPrimaryKey(comment.getUid()));
           comment.setGoods(goodsDao.selectByPrimaryKey(comment.getGid()));
            if (comment.getImages() != null) {
                comment.setImage(Arrays.asList(comment.getImages().split("~")));
            }
        }
        Page<Comment> page = new Page<Comment>();
        page.setItems(list);
        page.setPageNum(pageInfo.getPageNum());
        page.setPages(pageInfo.getPages());

        //展示分页10条页码
        // 1.一共展示10个页码，能够达到前5后4的效果
        // 2.如果前边不够5个，后边补齐10个
        // 3.如果后边不足4个，前边补齐10个
        Integer home = 0;  //开始
        Integer end = 0;   //结尾
        if (pageInfo.getPages()<10){  //如果总页数小于10,就把home设置为1,end设置为总页数
            home=1;
            end=pageInfo.getPages();
        }else{        //如果总页数大于10,就前5后4
            home = pageInfo.getPageNum()-5;
            end = pageInfo.getPageNum()+4;
            if (home<1){   //如果home小于1,就home设置为1，end设置为10
                home=1;
                end = 10;
            }
            if (end>pageInfo.getPages()){  //如果end大于总页数,end设置为总页数，home设置为end-9，前边补充
                end = pageInfo.getPages();
                home=end-9;
            }
        }
       page.setHome(home);
        page.setEnd(end);
        return page;
    }

    public Integer selectCount() {
        return commentDao.selectCount(null);
    }

    @Transactional
    public Integer delete(Long id) {
        Comment comment = commentDao.selectByPrimaryKey(id);
        if (comment==null){
            return 0;
        }
        if (comment.getPid()==0){
            Comment comm  = new Comment();
            comm.setPid(id);
            List<Comment> select = commentDao.select(comm);
            for (Comment comment1 : select) {
                commentDao.deleteByPrimaryKey(comment1.getId());
            }
        }
        return commentDao.deleteByPrimaryKey(id);
    }
}
